package com.cema.manage.project.android.biz;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.cema.manage.common.utils.MD5Utils;
import com.cema.manage.project.android.TestBaseContrller;
import com.cema.manage.project.android.model.AdCsjVo;
import com.cema.manage.project.android.model.AdTaKuVo;
import com.cema.manage.project.android.service.BizAdCallbackService;
import com.cema.manage.project.android.utils.DigestUtils;
import com.cema.manage.project.enums.RedisEnum;
import com.cema.manage.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequestMapping("ad_callback")
@RestController
public class BizAdCallbackController extends TestBaseContrller {
    private static final Logger log = LoggerFactory.getLogger(BizAdCallbackController.class);
    @Resource
    private BizAdCallbackService bizAdCallbackService;

    @Resource
    private RedisService redisService;

    /**
     * @param request
     * @return
     */
    @RequestMapping("csj")
    public JSONObject csjCallback(HttpServletRequest request) {
        AdCsjVo adCsjVo = (AdCsjVo) paramUtilsInterFase.getParam(request, AdCsjVo.class);
        String secretKey = (String) redisService.get(RedisEnum.AD_GM_SECRET_KEY.getKey());
        String sign = DigestUtils.getSHA256Hash(secretKey + ":" + adCsjVo.getTrans_id());
        log.info("回调密钥：" + adCsjVo.toString());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("is_verify", false);
        jsonObject.put("reason", -1);
        if (sign.equals(adCsjVo.getSign())) {
            boolean flag = bizAdCallbackService.createRewardRecord(adCsjVo);
            if (!flag) {
                return jsonObject;
            }
            jsonObject.put("is_verify", true);
            jsonObject.put("reason", 0);
        }
        log.info(jsonObject.toString());
        return jsonObject;
    }

    //快手
    @RequestMapping("taku")
    public void ksCallback(HttpServletRequest request, HttpServletResponse response) {
        AdTaKuVo adTaKuVo = (AdTaKuVo) paramUtilsInterFase.getParam(request, AdTaKuVo.class);
        AdCsjVo adCsjVo = new AdCsjVo();
        String ilrd = adTaKuVo.getIlrd();
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(ilrd);
        String showCustomExt = jsonObject.getStr("show_custom_ext");
        String scenarioId = jsonObject.getStr("scenario_id");
        String[] strings = showCustomExt.split(StrUtil.COMMA);
        String secretKey = (String) redisService.get(RedisEnum.TA_KU_AD_GM_SECRET_KEY.getKey()+strings[1]);
        String sign = "trans_id=" + adTaKuVo.getTrans_id() + "&placement_id=" + adTaKuVo.getPlacement_id() + "&adsource_id=" + adTaKuVo.getAdsource_id() + "&reward_amount=" + adTaKuVo.getReward_amount() + "&reward_name=" + adTaKuVo.getReward_name() + "&sec_key=" + secretKey + "&ilrd=" + adTaKuVo.getIlrd();
        sign = MD5Utils.getMD5String(sign);
        if (sign.equals(adTaKuVo.getSign())) {
            Double publisherRevenue = jsonObject.getDouble("publisher_revenue");
            adCsjVo.setUser_id(scenarioId);
            adCsjVo.setExtra(strings[0]);
            adCsjVo.setUvChannel(strings[1]);
            publisherRevenue = publisherRevenue * 100000;
            adCsjVo.setEcpm(String.valueOf(publisherRevenue.intValue()));
            boolean flag = bizAdCallbackService.createRewardRecord(adCsjVo);
            if (flag) {
                response.setStatus(200);
            } else {
                response.setStatus(602);
            }
        } else {
            response.setStatus(601);
        }
        log.info("Taku回调：" + adTaKuVo.toString());
    }

    //优量汇（腾讯）
    @RequestMapping("ylh")
    public void ylhCallback() {

    }
}
